package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class Lease here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lease
{
    private String id;
    private String start_date;
    private String return_date;
    private double total_cost;
    private ArrayList<Machine> machines;
    private boolean is_closed;

    /**
     * Constructor for objects of class Lease
     */
    public Lease(String i_id, String i_start_date, String i_return_date, double i_total_cost, ArrayList<String> machine_ids, boolean i_is_closed)
    {
        machines = new ArrayList<>();
        id = i_id;
        start_date = i_start_date;
        return_date = i_return_date;
        total_cost = i_total_cost;
        is_closed = i_is_closed;
        fill_in_machines(machine_ids);
        
    }
    /*************************************************GETTERS****************************************************/
    
    public String get_id()
    {
        return id;
    }
    
    public String get_start_date()
    {
        return start_date;
    }
    
    public String get_return_date()
    {
        return return_date;
    }
    
    public double get_total_cost()
    {
        return total_cost;
    }
    
    public boolean get_is_closed()
    {
        return is_closed;
    }
    
    public ArrayList<String> get_ids()
    {
        ArrayList<String> ids = new ArrayList<>();
        for(Machine machine : machines)
        {
            ids.add(machine.get_id());
        }        
        return ids;
    }
    
    
    /*************************************************SETTERS****************************************************/

    public void set_id(String input)
    {
        id = input;
    }
    
    public void set_start_date(String input)
    {
        start_date = input;
    }
    
    public void set_return_date(String input)
    {
        return_date = input;
    }
    
    public void set_total_cost(double input)
    {
        total_cost = input;
    }
    
    public void set_is_closed(boolean input)
    {
    	is_closed=input;
    	System.out.println(get_ids().get(0));
            for(Machine machine:machines)
            {
                machine.set_is_leased(false);
            }

    }
    
    
    /***************************************************************OTHER METHODS*****************************************************************/
    
    private void fill_in_machines(ArrayList<String> ids)
    {
        if(ids.contains("") == false)
        {
            for(String id : ids)
            {
                Machine temp = MachineCollection.find_by_id(id);
                if(!is_closed)
                {
                	temp.set_is_leased(true);
                }
                machines.add(temp);
            }
        }
    }
    
    public String give_string_data()
    {        
        return String.format("%s\t%s\t%s\t%s\t%s\t%s", id, start_date, return_date, total_cost, ids_maker(), is_closed);        
    }
    public Object[] give_array()
    {
    	Object[] attributes = { id, start_date, return_date, total_cost, machines, is_closed, CustomerCollection.get_customer_lease(id)};
    	return attributes;
    }
    
    private String convert_is_closed()
    {
        if(is_closed)
        {
            return "Closed";
        }
        return "Open";
    }
    
    private String ids_maker()
    {
        String str_ids = "";
        for (Machine machine : machines)
        {
            str_ids += machine.get_id() + " ";
        }
        return str_ids;
    }
    
    private String ids_printable()
    {
        String str_ids = "";
        boolean first = true;
        for(Machine machine : machines)
        {
            if(first)
            {
                str_ids += "\t\t\t" + machine.get_id() + "\n";
                first = false;
            }
            else
            {
                str_ids += "\t\t\t\t\t\t" + machine.get_id() + "\n";
            }
        }
        return str_ids;
    }
    
    private Machine find_machine(String id)
    {
        for(Machine machine : machines)
        {
            if(machine.get_id().equals(id))
            {
                return machine;
            }
        }
        return null;
    }
    
    public void clear_machine(String id)
    {
        machines.remove(find_machine(id));
    }
            
        
    
    

}
